<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col>
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
          <el-form-item label="手机号码" prop="mobileNumber">
            <el-input v-model="queryParams.mobileNumber" placeholder="请输入手机号码" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="短信内容" prop="content">
            <el-input v-model="queryParams.content" placeholder="请输入短信内容" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="发送状态" prop="sendStatusCode">
            <el-select @change="handleQuery" v-model="queryParams.sendStatusCode" size="small" clearable style="width: 150px" placeholder="请选择">
              <el-option key="" label="全部" value=""/>
              <el-option
                v-for="item in sendStatusCodeList"
                :key="item.dictCode"
                :label="item.dictName"
                :value="item.dictCode"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="发送时间">
            <el-date-picker @change="handleQuery" v-model="dateRange" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  size="mini" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button  size="mini" :disabled="buttonDisabled(total)" type="primary" icon="el-icon-bottom-right" @click="handleExport('thisPage')" v-hasPermi="['exportSmsRecordList']" >导出本页</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button  size="mini" :disabled="buttonDisabled(total)" type="primary" icon="el-icon-download" v-hasPermi="['exportSmsRecordList']" @click="handleExport('All')">导出全部</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="querySmsRecordPageResList" :columns="columns"></right-toolbar>
        </el-row>
        <el-table :stripe="true" v-loading="loading" :data="dataList" style="width: 100%" @sort-change='tableSortChange'>
          <el-table-column label="手机号码" align="left" prop="mobileNumber" v-if="columns[0].visible" width="150" />
          <el-table-column label="发送状态" align="left" prop="sendStatusName"  v-if="columns[1].visible"  width="150" >
            <template slot-scope="scope">
              <span :style="'color:'+scope.row.sendStatusColor">{{scope.row.sendStatusName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="短信内容" align="left" prop="content" v-if="columns[2].visible"/>
          <el-table-column label="发送时间" align="center" prop="createTime" v-if="columns[3].visible" sortable='custom'  width="200" />
          <el-table-column label="发送部门" align="left" prop="createDeptName"  v-if="columns[4].visible"/>
          <el-table-column label="发送IP" align="left" prop="sendIp" v-if="columns[5].visible" width="150" />
          <el-table-column label="备注" align="left" prop="remark" v-if="columns[6].visible"/>
        </el-table>
        <pagination v-show="total>0"  :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows" @pagination="querySmsRecordPageResList"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {querySmsRecordPageResList, exportSmsRecordPageResList} from '@/api/sms/smsRecord'
  import { getDicListByDictCodeList } from '@/api/common/dict'
  //默认查询参数
  let defaultQueryParams = {
    mobileNumber: undefined,
    startTime: undefined,
    endTime: undefined,
    sendStatusCode: "",
    content: undefined,
    page: 1,
    rows: 10
  }
  export default {
    name: 'smsRecordAdmin',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 显示搜索条件
        showSearch: true,
        //操作日志
        dataList: [],
        // 日期范围
        dateRange: [],
        //发送状态
        sendStatusCodeList: [],
        // 查询参数
        queryParams: { ...defaultQueryParams },
        //列信息
        columns: [
          { fieldCode: 'mobileNumber',fieldName: '手机号码',  visible: true },
          { fieldCode: 'sendStatusName',fieldName: '发送状态',  visible: true },
          { fieldCode: 'content',fieldName: '短信内容', visible: true },
          { fieldCode: 'createTime',fieldName: '发送时间',  visible: true },
          { fieldCode: 'createDeptName',fieldName: '发送部门',  visible: true },
          { fieldCode: 'sendIp', fieldName: '发送IP', visible: true },
          { fieldCode: 'remark', fieldName: '备注', visible: true },
        ]
      }
    },
    created() {
      this.querySmsRecordPageResList()
      this.initDict()
    },
    methods: {
      //排序
      tableSortChange(column) {
        //格式化排序参数，构建中
        this.orderByFormat(this.queryParams,column);
        //执行查询方法
        this.querySmsRecordPageResList()
      },
      /** 查询短信发送记录列表 */
      querySmsRecordPageResList() {
        this.loading = true
        querySmsRecordPageResList(this.addDateRange(this.queryParams, this.dateRange)).then(res => {
            this.dataList = res.data.records
            this.total = res.data.total
          }
        ).finally(() => {
          //关闭加载特效
          this.loading = false
        })
      },
      /** 重置按钮操作 */
      resetQuery(fromId) {
        this.dateRange = []
        this.resetForm(fromId)
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.querySmsRecordPageResList()
      },

      //获取数据字典
      initDict(){
        let dictArr = ["SMS_SEND_STATUS"];
        getDicListByDictCodeList(dictArr).then(res => {
          this.sendStatusCodeList = JSON.parse(JSON.stringify(res.data['SMS_SEND_STATUS']));
        });
      },
      /** 导出按钮操作 */
      handleExport(params) {
        //如果是导出本页则 去除分页参数
        if ('All' === params) {
          this.queryParams.rows = 0
        }
        let dataParams = { ...this.queryParams, ...{ excelFieldList: this.columns } }
        this.$confirm('是否确认导出数据?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          exportSmsRecordPageResList(dataParams, '短信发送记录')
        })
      }
    }
  }
</script>
